<template>
	<view>
		<view class="login">
			<view class="uni-common-mt">
				<view class="uni-form-item uni-column">
					<view class="login_label">欢迎来到AFFGO</view>
				</view>
				<view class="uni-form-item uni-column">
					<view>
						<input class="uni-input" v-model="username" focus placeholder="账号" />
					</view>
				</view>
				<view class="uni-form-item uni-column">
					<view>
						<input class="uni-input" v-model="password" hold-keyboard placeholder="密码" password=true>
					</view>
				</view>
				<view class="uni-form-item uni-column">
					<view>
						<button type="primary" @click="doLogin">登录</button>
					</view>
					<view style="margin-top: 10px;">
						<button v-if="regSwitch" type="default" @click="toReg">前往注册</button>
					</view>
					<view style="margin-top: 10px;">
						<button type="default" @click="toHome">回到首页</button>
					</view>
				</view>
			</view>
		</view>
		
		<zero-loading type="sword" v-if="loading" mask :maskOpacity="0.2"></zero-loading>
		
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import md5 from "md5";
	import ownApi from "@/api/ownApi.js";

	export default {
		data() {
			return {
				username: '',
				password: '',
				loading: false,
				type: 'center',
				msgType: 'success',
				messageText: '这是一条成功提示',
				regSwitch : false,
			}
		},
		async onLoad(){
			let res = await ownApi.getRegSwitch();
			if(res.code == 0){
				if(res.data.config_value == "1"){
					this.regSwitch = true;
				}else{
					this.regSwitch = false;
				}
			}
		},
		methods: {
			toReg(){
				uni.navigateTo({
					url:'/pages/own/reg'
				});
			},
			toHome() {
				uni.switchTab({
					url: "/pages/index/index"
				});
			},
			async doLogin() {
				this.loading = true;
				let param = {
					username: this.username,
					password: md5(this.password)
				};
				let res = await ownApi.doLogin(param);
				this.loading = false;
				if (res.code == 0) {
					uni.setStorageSync("token", res.data.token);
					uni.switchTab({
						url: "/pages/own/index"
					});

				} else {
					this.msgType = "error"
					this.messageText = res.msg;
					this.$refs.message.open();
				}
			}
		}
	}
</script>

<style lang="scss">
	.login {
		padding: 20rpx 40rpx;
		margin: 200rpx 20rpx;
		text-align: center;
		background-color: #eee;

		.login_label {
			color: #333333;
			margin: 0 auto;
		}

		.uni-input {
			background-color: #fff;
			height: 80rpx;
			text-align: left;
			padding-left: 10rpx;
			border-radius: 10rpx;
			font-size: 30rpx;
		}

		.uni-form-item {
			margin: 30rpx 0;
		}

		button {
			font-size: 30rpx;
		}
	}
</style>